package com.ruoyi.community.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.community.domain.CommunityComments;
import com.ruoyi.community.mapper.CommunityPostMapper;
import com.ruoyi.community.domain.CommunityPost;
import com.ruoyi.community.service.ICommunityPostService;

/**
 * 社区帖子Service业务层处理
 *
 * @author FeiYiLand
 * @date 2024-07-14
 */
@Service
public class CommunityPostServiceImpl implements ICommunityPostService
{
    @Autowired
    private CommunityPostMapper communityPostMapper;


    /**
     * 查询社区帖子
     *
     * @param postId 社区帖子主键
     * @return 社区帖子
     */
    @Override
    public CommunityPost selectCommunityPostByPostId(Long postId)
    {
        return communityPostMapper.selectCommunityPostByPostId(postId);
    }

    /**
     * 查询用户信息
     */
    /**
     * 查询社区帖子列表
     *
     * @param communityPost 社区帖子
     * @return 社区帖子
     */
    @Override
    public List<CommunityPost> selectCommunityPostList(CommunityPost communityPost)
    {
        return communityPostMapper.selectCommunityPostList(communityPost);
    }

    /**
     * 新增社区帖子
     *
     * @param communityPost 社区帖子
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCommunityPost(CommunityPost communityPost)
    {
        communityPost.setCreateTime(DateUtils.getNowDate());
        int rows = communityPostMapper.insertCommunityPost(communityPost);
        insertCommunityComments(communityPost);
        return rows;
    }

    /**
     * 修改社区帖子
     *
     * @param communityPost 社区帖子
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCommunityPost(CommunityPost communityPost)
    {
        communityPostMapper.deleteCommunityCommentsByCommentsPostId(communityPost.getPostId());
        insertCommunityComments(communityPost);
        return communityPostMapper.updateCommunityPost(communityPost);
    }

    /**
     * 批量删除社区帖子
     *
     * @param postIds 需要删除的社区帖子主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCommunityPostByPostIds(Long[] postIds)
    {
        communityPostMapper.deleteCommunityCommentsByCommentsPostIds(postIds);
        return communityPostMapper.deleteCommunityPostByPostIds(postIds);
    }

    /**
     * 删除社区帖子信息
     *
     * @param postId 社区帖子主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCommunityPostByPostId(Long postId)
    {
        communityPostMapper.deleteCommunityCommentsByCommentsPostId(postId);
        return communityPostMapper.deleteCommunityPostByPostId(postId);
    }

    /**
     * 新增社区评论信息
     *
     * @param communityPost 社区帖子对象
     */
    public void insertCommunityComments(CommunityPost communityPost)
    {
        List<CommunityComments> communityCommentsList = communityPost.getCommunityCommentsList();
        Long postId = communityPost.getPostId();
        if (StringUtils.isNotNull(communityCommentsList))
        {
            List<CommunityComments> list = new ArrayList<CommunityComments>();
            for (CommunityComments communityComments : communityCommentsList)
            {
                communityComments.setCommentsPostId(postId);
                list.add(communityComments);
            }
            if (list.size() > 0)
            {
                communityPostMapper.batchCommunityComments(list);
            }
        }
    }
}
